//
//  BYVisitorViewController.m
//  School+
//
//  Created by Sean Sebastian on 5/19/16.
//  Copyright © 2016 Sean Sebastian. All rights reserved.
//

#import "BYVisitorViewController.h"

#import "masonry.h"

#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>

#import "BYRegisterViewController.h"
#import "BYLoginControllerViewController.h"

@interface BYVisitorViewController ()

@property (nonatomic , weak) UIButton *signUp;
@property (nonatomic , weak) UIButton *signIn;

@property(nonatomic ,strong)AVAudioSession *avaudioSession;

@property(nonatomic,strong)MPMoviePlayerController *moviePlayer;

@end

@implementation BYVisitorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     *  设置其他音乐软件播放的音乐不被打断
     */

    
    self.avaudioSession = [AVAudioSession sharedInstance];
    NSError *error = nil;
    [self.avaudioSession setCategory:AVAudioSessionCategoryAmbient error:&error];
    
    
    
    NSString *urlStr = [[NSBundle mainBundle]pathForResource:@"1.MOV" ofType:nil];
    
    NSURL *url = [NSURL fileURLWithPath:urlStr];
    
    _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
    //    _moviePlayer.controlStyle = MPMovieControlStyleDefault;
    [_moviePlayer play];
    [_moviePlayer.view setFrame:self.view.bounds];
    
    [self.view addSubview:_moviePlayer.view];
    _moviePlayer.shouldAutoplay = YES;
    [_moviePlayer setControlStyle:MPMovieControlStyleNone];
    [_moviePlayer setFullscreen:YES];
    
    [_moviePlayer setRepeatMode:MPMovieRepeatModeOne];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playbackStateChanged)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:_moviePlayer];
    
    

    

    
    [self prepareUI];
    
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)prepareUI
{
    [self.view addSubview:self.signUp];
    [self.view addSubview:self.signIn];
    
    
    [self.signIn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-20);
        make.left.equalTo(self.view.mas_left).with.offset(20);
        make.height.mas_equalTo(@50);
        make.right.equalTo(self.view.mas_centerX).with.offset(-5);
        
    }];
    
    
    [self.signUp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-20);
        make.right.equalTo(self.view.mas_right).with.offset(-20);
        make.height.mas_equalTo(@50);
        make.left.equalTo(self.view.mas_centerX).with.offset(5);
    }];
}

- (UIButton *)signUp
{
    if (_signUp == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"注册" forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor blackColor];
        btn.layer.cornerRadius = 3.0f;
        btn.alpha = 0.4f;
        [btn addTarget:self action:@selector(signUpClick) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:18];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        _signUp = btn;
        return btn;
        
    }
    
    return _signUp;
}

- (UIButton *)signIn
{
    if (_signIn == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"登陆" forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor whiteColor];
        btn.layer.cornerRadius = 3.0f;
        btn.alpha = 0.4f;
        [btn addTarget:self action:@selector(signInClick) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:18];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        _signIn = btn;
        return btn;
    }
    
    return _signIn;
}


-(void)playbackStateChanged{
    
    
    //取得目前状态
    MPMoviePlaybackState playbackState = [_moviePlayer playbackState];
    
    //状态类型
    switch (playbackState) {
        case MPMoviePlaybackStateStopped:
            [_moviePlayer play];
            break;
            
        case MPMoviePlaybackStatePlaying:
            NSLog(@"播放中");
            break;
            
        case MPMoviePlaybackStatePaused:
            [_moviePlayer play];
            break;
            
        case MPMoviePlaybackStateInterrupted:
            NSLog(@"播放被中断");
            break;
            
        case MPMoviePlaybackStateSeekingForward:
            NSLog(@"往前快转");
            break;
            
        case MPMoviePlaybackStateSeekingBackward:
            NSLog(@"往后快转");
            break;
            
        default:
            NSLog(@"无法辨识的状态");
            break;
    }
}


-(BOOL)prefersStatusBarHidden{
    
    return YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}


- (void)signUpClick
{
    // 创建控制器
    BYRegisterViewController *vc = [[BYRegisterViewController alloc] init];
    // 创建导航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    vc.title = @"注册";
    [self presentViewController:nav animated:YES completion:nil];

}
- (void)signInClick
{
    
    // 创建控制器
    BYLoginControllerViewController *vc = [[BYLoginControllerViewController alloc] init];
    // 创建导航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    vc.title = @"登陆";
    [self presentViewController:nav animated:YES completion:nil];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.moviePlayer stop];
    NSLog(@"暂停播放");
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.moviePlayer play];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
